using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;

namespace SolanumSolstice.Buildings
{
    public class ResearchBuilding : Building
    {

        protected List<Researcher> researchers;
        protected ResearchTree researchTree;
        protected int totalResearch;
        protected int researchNeeded;
        protected bool isResearching;
        protected bool researchTreeIsVisible;
        protected UnitClass unitClassResearch;

        #region Accessors
        public List<Researcher> Researchers
        {
            get
            {
                return researchers;
            }
            set
            {
                researchers = value;
            }
        }

        public bool IsResearching
        {
            get
            {
                return isResearching;
            }
            set
            {
                isResearching = value;
            }  
        }

        public bool ResearchTreeIsVisible
        {
            get
            {
                return researchTreeIsVisible;
            }
            set
            {
                researchTreeIsVisible = value;
            }
        }

        public int TotalResearch
        {
            get
            {
                return totalResearch;
            }
            set
            {
                totalResearch = value;
            }
        }

        public int ResearchNeeded
        {
            get
            {
                return researchNeeded;
            }
            set
            {
                researchNeeded = value;
            }
        }

        public UnitClass UnitClassResearch
        {
            get
            {
                return unitClassResearch;
            }
            set
            {
                unitClassResearch = value;
            }
        }
        #endregion



        public ResearchBuilding(ContentManager content, string name, Vector2 position, Vector2 entrance, int zombieCount)
            : base(content, name, position, entrance, zombieCount)
        {
            buildingType = BuildingType.Research;
            researchers = new List<Researcher>();
            researchTree = new ResearchTree(content, this);
            researchTreeIsVisible = false;
            unitClassResearch = UnitClass.Civilian;

            texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_research_library");
            width = texture.Width;
            height = texture.Height;

            //fullBar = content.Load<Texture2D>("Content\\Txtr\\Units\\FullBarWhite");
            //emptyBar = content.Load<Texture2D>("Content\\Txtr\\Units\\EmptyBar");
        }

        public override void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            List<Researcher> removeList = new List<Researcher>();

            foreach (Researcher researcher in researchers)
            {
                if (researcher.IsResearching == false || researcher.IsDead == true)
                {
                    removeList.Add(researcher);
                }
            }

            foreach (Researcher gone in removeList)
            {
                researchers.Remove(gone);
            }
            
            base.Update(gameTime, gamePad, gameCursor);
            
            if (gamePad.Triggers.Left > 0)
            {
                researchTreeIsVisible = true;
                researchTree.Update(gameTime, gamePad, gameCursor);

            }
            else
            {
                researchTreeIsVisible = false;
            }


            if (!isInfested)
            {
                if (isResearching == true)
                {
                    foreach (Researcher researcher in researchers)
                    {
                        if (researcher.ActionReady == true)
                        {
                            totalResearch += researcher.ResearchPower;
                            researcher.ActionReady = false;
                            researcher.RecoveryTime = researcher.ResearchTime;
                            researcher.InitialRecoveryTime = researcher.ResearchTime;
                        }
                    }
                    if (totalResearch >= researchNeeded)
                    {
                        isResearching = false;
                        researchTree.Upgrade(unitClassResearch);
                        ExitResearchers();
                        SolanumSolstice.soundBank.PlayCue("research_complete");
                    }
                }

                
            }


        }

        public override void DrawHUD(SpriteBatch spritebatch, Rectangle drawBoundary)
        {
            base.DrawHUD(spritebatch, drawBoundary);

            if (isResearching == true)
            {
                Color researchColor = Color.Green;

                int researchBarLength = (int)(((float)totalResearch / (float)researchNeeded) * 100);

                if (totalResearch < ((researchNeeded * 2) / 3))
                {
                    researchColor = Color.Yellow;
                }
                if (totalResearch < (researchNeeded / 3))
                {
                    researchColor = Color.Red;
                }

                string hudString = GetHUDString();

                spritebatch.DrawString(smallFont, GetHUDString(), new Vector2(drawBoundary.X - 15, drawBoundary.Y), Color.White);

//                List<string> description = researchTree.GetDescription(unitClassResearch);
                string description = researchTree.GetDescription(unitClassResearch);
                //foreach (string s in description)
                //{
                //    spritebatch.DrawString(smallFont, s, new Vector2(drawBoundary.X - 15, drawBoundary.Y + 50 + (12 * description.IndexOf(s))), Color.White);
                //}
                spritebatch.DrawString(smallFont, description, new Vector2(drawBoundary.X - 15, drawBoundary.Y + 50 + (12)), Color.White);

                spritebatch.Draw(emptyBar, new Rectangle(drawBoundary.X + 10, drawBoundary.Y + 30, 100, 10), Color.White);
                spritebatch.Draw(fullBar, new Rectangle(drawBoundary.X + 10, drawBoundary.Y + 30, researchBarLength, 10), researchColor);
            }

            else
            {
                spritebatch.DrawString(smallFont, "No active research", new Vector2(drawBoundary.X, drawBoundary.Y + 10), Color.White);
                spritebatch.DrawString(smallFont, "Press Left Trigger to choose", new Vector2(drawBoundary.X, drawBoundary.Y + 25), Color.White);
                spritebatch.DrawString(smallFont, "new upgrade to research.", new Vector2(drawBoundary.X, drawBoundary.Y + 40), Color.White);
                
            }

            spritebatch.DrawString(smallFont, "Current Researchers: " + researchers.Count, new Vector2(drawBoundary.X - 10, drawBoundary.Y + 80), Color.Red);

            if (researchTreeIsVisible)
            {
                researchTree.Draw(spritebatch);
            }
        }

        public void StartNewResearch(UnitClass unitClass)
        {
            if (isResearching == false)
            {
                totalResearch = 0;
                isResearching = true;

                if (unitClass == UnitClass.Civilian && researchTree.CivilianLevel <= 9)
                {
                    researchTree.CivilianLevel++;
                    researchNeeded = (researchTree.CivilianLevel + 1) * 50;
                    unitClassResearch = UnitClass.Civilian;

                }
                else if (unitClass == UnitClass.Military && researchTree.MilitaryLevel <= 9)
                {
                    researchTree.MilitaryLevel++;
                    researchNeeded = (researchTree.MilitaryLevel + 1) * 50;
                    unitClassResearch = UnitClass.Military;
                }
                else if (unitClass == UnitClass.Science && researchTree.ScienceLevel <= 9)
                {
                    researchTree.ScienceLevel++;
                    researchNeeded = (researchTree.ScienceLevel + 1) * 50;
                    unitClassResearch = UnitClass.Science;
                }
            }
        }

        public void EnterBuildingAsResearcher(Researcher researcher)
        {
            researcher.InBuilding = true;
            researchers.Add(researcher);
        }

        public void ExitResearchers()
        {
            foreach (Researcher researcher in researchers)
            {
                researcher.InBuilding = false;
                researcher.IsResearching = false;
            }
        }

        public string GetHUDString()
        {
            string final = "";

            if(unitClassResearch == UnitClass.Civilian)
            {
              final = "Researching Civilian Lvl " + (researchTree.CivilianLevel);
            }
            else if(unitClassResearch == UnitClass.Military)
            {
                final = "Researching Military Lvl " + (researchTree.MilitaryLevel);
            }
            else
            {
                final = "Researching Science Lvl " + (researchTree.ScienceLevel);
            }

            return final;
        }



    }
}
